﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ShopBLL;
using ShopModel;

    /// <summary>
    /// Kontrolka menu kategorii
    /// </summary>
    /// <remarks>
    /// Autor: Paweł Janiszewski
    /// Data utworzenia: 8.05.2008
    /// Ostatnia modyfikacja: 9.05.2008 - Paweł Janiszewski
    /// </remarks>
    public partial class ctrlMenu : System.Web.UI.UserControl
    {
        #region VARIABLES

        private CategoryList dataList;
        private int openCategoryId;
        private int trigger = 0;
        private bool expanded;

        #endregion VARIABLES

        #region PROPERTIES

        /// <summary>
        /// Dane na podstawie, których zostanie wygenerowane menu
        /// </summary>
        public CategoryList DataList
        {
            get { return dataList; }
            set { dataList = value; }
        }

        /// <summary>
        /// Id otwartej kategorii
        /// </summary>
        public int OpenCategoryId
        {
            get { return openCategoryId; }
            set { openCategoryId = value; }
        }

        public bool Expanded
        {
            get { return expanded; }
            set { expanded = value; }
        }

        #endregion PROPERTIES

        #region METHODS

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            SetOpenCategory(dataList);
            GenerateCategory(writer, dataList);
        }

        /// <summary>
        /// Rozpoczyna rekurencyjne generowanie menu, na podstawie itemList
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="itemList">Kolekcja kategorii na podstawie której generowane jest menu</param>
        private void GenerateCategory(HtmlTextWriter writer, CategoryList itemList)
        {
            if (dataList != null)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, "MenuAccordion");
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "sidemenu");
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                for (int i = 0; i < dataList.Count; i++)
                {
                    GenerateSubcategory(writer, dataList[i], "c" + dataList[i].CategoryID.ToString(), 1);
                }
                writer.RenderEndTag();
            }
        }

        /// <summary>
        /// Rekurencyjne generowanie podkategorii
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="item">Klasa podkategorii</param>
        /// <param name="id">Identyfikator podkategorii</param>
        private void GenerateSubcategory(HtmlTextWriter writer, CategoryListItem item, string id, int depth)
        {
            trigger++;

            if (trigger > 10000)
                return;

            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            if (item.IsSubcategory)
            {
                if (item.IsOpen)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, Resources.Language.Collapse);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "gfx/minus.gif");
                }
                else
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, Resources.Language.Expand);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "gfx/plus.gif");
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "toggleMenu('" + id + "');ChangeImage(event);return false;");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
                writer.AddAttribute(HtmlTextWriterAttribute.Href, item.Url);
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "margin-left: 3px;");
                //writer.AddAttribute(HtmlTextWriterAttribute.Class, "MenuAccordionHeader");

                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(item.Text);
                writer.RenderEndTag();
                writer.AddAttribute(HtmlTextWriterAttribute.Id, "menuSubCat" + id);

                if (!Expanded && !item.IsOpen)
                {
                    //   writer.AddAttribute(HtmlTextWriterAttribute.Class, "SubcategoryLevel"+depth.ToString());
                    //   writer.AddAttribute(HtmlTextWriterAttribute.Class, "subcategory" );
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "display:none;");
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "SubcategoryLevel" + depth.ToString());

                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                for (int i = 0; i < item.Items.Count; i++)
                {
                    GenerateSubcategory(writer, item[i], id + "s" + item[i].CategoryID.ToString(), depth + 1);
                }
                writer.RenderEndTag();
            }
            else
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Alt, "");
                writer.AddAttribute(HtmlTextWriterAttribute.Src, "gfx/arrow.gif");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, item.Url);
                //writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "toggleMenu('" + id + "');return false;");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
                writer.AddAttribute(HtmlTextWriterAttribute.Href, item.Url);
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "margin-left: 3px;");
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(item.Text);
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
        }

        /// <summary>
        /// Rozpoczyna rekurencyjne ustawianie otwartych kategorii
        /// </summary>
        /// <param name="categoryList"></param>
        private void SetOpenCategory(CategoryList categoryList)
        {
            foreach (CategoryListItem current in categoryList)
            {
                if (current.IsSubcategory)
                {
                    current.IsOpen = HasOpenChildren(current) || current.CategoryID == openCategoryId;
                }
                else
                {
                    current.IsOpen = current.CategoryID == openCategoryId;
                }
            }
        }

        /// <summary>
        /// Sprawdza czy któreś z dzieci jest otwartą kategorią i jesli tak to ustawia całą ścieżkę na otwartą
        /// </summary>
        /// <param name="categoryList"></param>
        /// <returns>Czy któreś z dzieci jest otwarte</returns>
        private bool HasOpenChildren(CategoryListItem categoryList)
        {
            bool result = false;
            foreach (CategoryListItem current in categoryList.Items)
            {
                if (current.IsSubcategory)
                {
                    current.IsOpen = (current.CategoryID == openCategoryId || HasOpenChildren(current));
                }
                else
                {
                    current.IsOpen = (current.CategoryID == openCategoryId);
                }

                result = result || current.IsOpen;
            }
            return result;
        }

        #endregion METHODS
    }
